﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IndoReputation.Models.BusinessObject;
using IndoReputation.Models.BusinessObject.Collections;
using IndoReputation.Models.DataAccess;
using IndoReputation.Views.ViewModels;
using IndoReputation.Models.Constants;
using IndoReputation.Commons;

namespace IndoReputation.Models.BusinessLogic
{
    public class MediaBOL
    {
        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 4/22/2012  6:03 PM
        /// Todo: Insert an media to db
        /// </summary>
        /// <param name="media">The media.</param>
        /// <returns></returns>
        public static int Insert(Media media)
        {
            try
            {
                media.CreateAt = DateTime.Now;
                media.CreateBy = MySession.UserID;
                media.UpdateAt = null;
                media.UpdateBy = null;
                return MediaDAL.Insert(media);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/30/2011  9:45 AM
        /// Todo: Update a User Exists in Database 
        /// </summary>
        /// <param name="media">A media.</param>
        /// <returns></returns>
        public static bool Update(Media media)
        {
            try
            {
                media.UpdateAt = DateTime.Now;
                media.UpdateBy = MySession.UserID;
                return MediaDAL.Update(media);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/30/2011  10:36 AM
        /// Todo: Delete a User Existst in Database
        /// </summary>
        /// <param name="media">A media.</param>
        /// <returns></returns>
        public static bool Delete(int mediaID)
        {
            try
            {
                return MediaDAL.Delete(mediaID);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/29/2011  5:23 PM
        /// Todo: Get All Media
        /// </summary>
        /// <returns></returns>
        public static MediaList GetAllMedia()
        {
            try
            {
                return MediaDAL.GetAllMedia() ?? new MediaList();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/30/2011  10:40 AM
        /// Todo: Select a User existst in Database
        /// </summary>
        /// <param name="mediaID">The media ID.</param>
        /// <returns></returns>
        public static Media GetMediaById(int mediaID)
        {
            try
            {
                return MediaDAL.GetMediaById(mediaID);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/29/2011  5:23 PM
        /// Todo: Get All Media
        /// </summary>
        /// <returns></returns>
        public static MediaJoinList GetAllMediaJoin()
        {
            try
            {
                return MediaDAL.GetAllMediaJoin() ?? new MediaJoinList();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        public static MediaList GetAllMediaTopN(int top = 10, int countryID = 0)
        {
            try
            {
                return MediaDAL.GetAllMediaTopN(top, countryID) ?? new MediaList();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        public static Int64 GetCountChart(DateTime dtFrom, DateTime dtTo, int regionID = 0, int countryID = 0)
        {
            try
            {
                return MediaDAL.GetCountChart(dtFrom, dtTo, regionID, countryID);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        public static List<ChartMediaData> GetChartData(DateTime dtFrom, DateTime dtTo, IRConstants.DisplayBy displayBy = IRConstants.DisplayBy.Month)
        {
            List<ChartMediaData> result = new List<ChartMediaData>();
            switch (displayBy)
            {
                case IRConstants.DisplayBy.Month:
                    int totalMonth = ((dtTo.Year - dtFrom.Year) * 12) + dtTo.Month - dtFrom.Month + 1;
                    for (int i = 0; i < totalMonth; i++)
                    {
                        ChartMediaData c = new ChartMediaData();
                        DateTime dtTemp = dtFrom.AddMonths(i);
                        c.Totals = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, 1, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, DateTime.DaysInMonth(dtTemp.Year, dtTemp.Month), 23, 59, 59));                        
                        c.DateString = dtTemp.ToString("MMM yy");
                        result.Add(c);
                    }
                    break;
                case IRConstants.DisplayBy.Year:
                    int totalYear = dtTo.Year - dtFrom.Year + 1;
                    for (int i = 0; i < totalYear; i++)
                    {
                        ChartMediaData c = new ChartMediaData();
                        DateTime dtTemp = dtFrom.AddYears(i);
                        c.Totals = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59));
                        c.DateString = dtTemp.ToString("yyyy");
                        result.Add(c);
                    }
                    break;
            }
            return result;
        }

        public static List<ChartMediaData> GetChartDataCountry(int countryID, DateTime dtFrom, DateTime dtTo, IRConstants.DisplayBy displayBy = IRConstants.DisplayBy.Month)
        {
            List<ChartMediaData> result = new List<ChartMediaData>();
            switch (displayBy)
            {
                case IRConstants.DisplayBy.Month:
                    int totalMonth = ((dtTo.Year - dtFrom.Year) * 12) + dtTo.Month - dtFrom.Month + 1;
                    for (int i = 0; i < totalMonth; i++)
                    {
                        ChartMediaData c = new ChartMediaData();
                        DateTime dtTemp = dtFrom.AddMonths(i);
                        c.Totals = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, 1, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, DateTime.DaysInMonth(dtTemp.Year, dtTemp.Month), 23, 59, 59), 0, countryID);                        
                        c.DateString = dtTemp.ToString("MMM yy");
                        result.Add(c);
                    }
                    break;
                case IRConstants.DisplayBy.Year:
                    int totalYear = dtTo.Year - dtFrom.Year + 1;
                    for (int i = 0; i < totalYear; i++)
                    {
                        ChartMediaData c = new ChartMediaData();
                        DateTime dtTemp = dtFrom.AddYears(i);
                        c.Totals = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59), 0, countryID);                       
                        c.DateString = dtTemp.ToString("yyyy");
                        result.Add(c);
                    }
                    break;
            }
            return result;
        }

        public static List<ChartMediaData> GetChartDataRegion(int regionID, DateTime dtFrom, DateTime dtTo, IRConstants.DisplayBy displayBy = IRConstants.DisplayBy.Month)
        {
            List<ChartMediaData> result = new List<ChartMediaData>();
            switch (displayBy)
            {
                case IRConstants.DisplayBy.Month:
                    int totalMonth = ((dtTo.Year - dtFrom.Year) * 12) + dtTo.Month - dtFrom.Month + 1;
                    for (int i = 0; i < totalMonth; i++)
                    {
                        ChartMediaData c = new ChartMediaData();
                        DateTime dtTemp = dtFrom.AddMonths(i);
                        c.Totals = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, 1, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, DateTime.DaysInMonth(dtTemp.Year, dtTemp.Month), 23, 59, 59), regionID, 0);                        
                        c.DateString = dtTemp.ToString("MMM yy");
                        result.Add(c);
                    }
                    break;
                case IRConstants.DisplayBy.Year:
                    int totalYear = dtTo.Year - dtFrom.Year + 1;
                    for (int i = 0; i < totalYear; i++)
                    {
                        ChartMediaData c = new ChartMediaData();
                        DateTime dtTemp = dtFrom.AddYears(i);
                        c.Totals = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59), regionID, 0);
                        c.DateString = dtTemp.ToString("yyyy");
                        result.Add(c);
                    }
                    break;
            }
            return result;
        }

        public static TrendNewsModel GetTrendNews(DateTime dtTrend)
        {
            TrendNewsModel result = new TrendNewsModel();
            // trendTodayLastyear
            TrendCompare trendTodayLastyear = new TrendCompare();
            DateTime dtTemp = dtTrend;
            trendTodayLastyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 23, 59, 59, 999));
            dtTemp = dtTrend.AddYears(-1);
            trendTodayLastyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 23, 59, 59, 999));
            trendTodayLastyear.trend = CommonFunctions.CompareTrend(trendTodayLastyear.totalLast, trendTodayLastyear.totalCurrent);
            result.trendTodayLastyear = trendTodayLastyear;
            // trendThisWeekLastyear
            TrendCompare trendThisWeekLastyear = new TrendCompare();
            dtTemp = CommonFunctions.GetFirstDayOfWeek(dtTrend);
            DateTime dtTempWeek = dtTemp.AddDays(7);
            trendThisWeekLastyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTempWeek.Year, dtTempWeek.Month, dtTempWeek.Day, 23, 59, 59, 999));
            dtTemp = CommonFunctions.GetFirstDayOfWeek(dtTrend.AddYears(-1));
            dtTempWeek = dtTemp.AddDays(7);
            trendThisWeekLastyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTempWeek.Year, dtTempWeek.Month, dtTempWeek.Day, 23, 59, 59, 999));
            trendThisWeekLastyear.trend = CommonFunctions.CompareTrend(trendThisWeekLastyear.totalLast, trendThisWeekLastyear.totalCurrent);
            result.trendThisWeekLastyear = trendThisWeekLastyear;
            // trendThisMonthLastyear
            TrendCompare trendThisMonthLastyear = new TrendCompare();
            dtTemp = dtTrend;
            trendThisMonthLastyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, DateTime.DaysInMonth(dtTemp.Year, dtTemp.Month), 23, 59, 59, 999));
            dtTemp = dtTrend.AddYears(-1);
            trendThisMonthLastyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, DateTime.DaysInMonth(dtTemp.Year, dtTemp.Month), 23, 59, 59, 999));
            trendThisMonthLastyear.trend = CommonFunctions.CompareTrend(trendThisMonthLastyear.totalLast, trendThisMonthLastyear.totalCurrent);
            result.trendThisMonthLastyear = trendThisMonthLastyear;
            // trendThisYearLastyear
            TrendCompare trendThisYearLastyear = new TrendCompare();
            dtTemp = dtTrend;
            trendThisYearLastyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999));
            dtTemp = dtTrend.AddYears(-1);
            trendThisYearLastyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999));
            trendThisYearLastyear.trend = CommonFunctions.CompareTrend(trendThisYearLastyear.totalLast, trendThisMonthLastyear.totalCurrent);
            result.trendThisYearLastyear = trendThisYearLastyear;
            // trendTodayThisyear
            TrendCompare trendTodayThisyear = new TrendCompare();
            dtTemp = dtTrend;
            trendTodayThisyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 23, 59, 59, 999));
            dtTemp = dtTemp.AddDays(-1);
            trendTodayThisyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 23, 59, 59, 999));
            trendTodayThisyear.trend = CommonFunctions.CompareTrend(trendTodayThisyear.totalLast, trendTodayThisyear.totalCurrent);
            result.trendTodayThisyear = trendTodayThisyear;
            // trendThisWeekThisyear
            TrendCompare trendThisWeekThisyear = new TrendCompare();
            dtTemp = CommonFunctions.GetFirstDayOfWeek(dtTrend);
            dtTempWeek = dtTemp.AddDays(7);
            trendThisWeekThisyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTempWeek.Year, dtTempWeek.Month, dtTempWeek.Day, 23, 59, 59, 999));
            // random sub day from 1-6
            dtTemp = CommonFunctions.GetFirstDayOfWeek(dtTemp.AddDays(-4));
            dtTempWeek = dtTemp.AddDays(7);
            trendThisWeekThisyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTempWeek.Year, dtTempWeek.Month, dtTempWeek.Day, 23, 59, 59, 999));
            trendThisWeekThisyear.trend = CommonFunctions.CompareTrend(trendThisWeekThisyear.totalLast, trendThisWeekThisyear.totalCurrent);
            result.trendThisWeekThisyear = trendThisWeekThisyear;
            // trendThisMonthThisyear
            TrendCompare trendThisMonthThisyear = new TrendCompare();
            dtTemp = dtTrend;
            trendThisMonthThisyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, DateTime.DaysInMonth(dtTemp.Year, dtTemp.Month), 23, 59, 59, 999));
            dtTemp = dtTrend.AddMonths(-1);
            trendThisMonthThisyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, DateTime.DaysInMonth(dtTemp.Year, dtTemp.Month), 23, 59, 59, 999));
            trendThisMonthThisyear.trend = CommonFunctions.CompareTrend(trendThisMonthThisyear.totalLast, trendThisMonthThisyear.totalCurrent);
            result.trendThisMonthThisyear = trendThisMonthThisyear;
            // trendThisYearThisyear
            result.trendThisYearThisyear = trendThisYearLastyear;
            // TrendCompareYear trendYear1 // trendYear2 // trendYear3 // trendYear4 // trendYear5
            // trendYear1
            TrendCompareYear tcYear1 = new TrendCompareYear();
            dtTemp = dtTrend.AddYears(-4);
            tcYear1.year = dtTemp.Year;
            tcYear1.total = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999));
            tcYear1.trend = 0;
            result.trendYear1 = tcYear1;
            // trendYear2
            TrendCompareYear tcYear2 = new TrendCompareYear();
            dtTemp = dtTrend.AddYears(-3);
            tcYear2.year = dtTemp.Year;
            tcYear2.total = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999));
            tcYear2.trend = CommonFunctions.CompareTrend(tcYear1.total, tcYear2.total);
            result.trendYear2 = tcYear2;
            // trendYear3
            TrendCompareYear tcYear3 = new TrendCompareYear();
            dtTemp = dtTrend.AddYears(-2);
            tcYear3.year = dtTemp.Year;
            tcYear3.total = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999));
            tcYear3.trend = CommonFunctions.CompareTrend(tcYear2.total, tcYear3.total);
            result.trendYear3 = tcYear3;
            // trendYear4
            TrendCompareYear tcYear4 = new TrendCompareYear();
            dtTemp = dtTrend.AddYears(-1);
            tcYear4.year = dtTemp.Year;
            tcYear4.total = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999));
            tcYear4.trend = CommonFunctions.CompareTrend(tcYear3.total, tcYear4.total);
            result.trendYear4 = tcYear4;
            // trendYear5
            TrendCompareYear tcYear5 = new TrendCompareYear();
            dtTemp = dtTrend;
            tcYear5.year = dtTemp.Year;
            tcYear5.total = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999));
            tcYear5.trend = CommonFunctions.CompareTrend(tcYear4.total, tcYear5.total);
            result.trendYear5 = tcYear5;
            return result;
        }

        public static TrendNewsModel GetTrendNewsCountry(DateTime dtTrend, int countryID)
        {
            TrendNewsModel result = new TrendNewsModel();
            // trendTodayLastyear
            TrendCompare trendTodayLastyear = new TrendCompare();
            DateTime dtTemp = dtTrend;
            trendTodayLastyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 23, 59, 59, 999), 0, countryID);
            dtTemp = dtTrend.AddYears(-1);
            trendTodayLastyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 23, 59, 59, 999), 0, countryID);
            trendTodayLastyear.trend = CommonFunctions.CompareTrend(trendTodayLastyear.totalLast, trendTodayLastyear.totalCurrent);
            result.trendTodayLastyear = trendTodayLastyear;
            // trendThisWeekLastyear
            TrendCompare trendThisWeekLastyear = new TrendCompare();
            dtTemp = CommonFunctions.GetFirstDayOfWeek(dtTrend);
            DateTime dtTempWeek = dtTemp.AddDays(7);
            trendThisWeekLastyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTempWeek.Year, dtTempWeek.Month, dtTempWeek.Day, 23, 59, 59, 999), 0, countryID);
            dtTemp = CommonFunctions.GetFirstDayOfWeek(dtTrend.AddYears(-1));
            dtTempWeek = dtTemp.AddDays(7);
            trendThisWeekLastyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTempWeek.Year, dtTempWeek.Month, dtTempWeek.Day, 23, 59, 59, 999), 0, countryID);
            trendThisWeekLastyear.trend = CommonFunctions.CompareTrend(trendThisWeekLastyear.totalLast, trendThisWeekLastyear.totalCurrent);
            result.trendThisWeekLastyear = trendThisWeekLastyear;
            // trendThisMonthLastyear
            TrendCompare trendThisMonthLastyear = new TrendCompare();
            dtTemp = dtTrend;
            trendThisMonthLastyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, DateTime.DaysInMonth(dtTemp.Year, dtTemp.Month), 23, 59, 59, 999), 0, countryID);
            dtTemp = dtTrend.AddYears(-1);
            trendThisMonthLastyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, DateTime.DaysInMonth(dtTemp.Year, dtTemp.Month), 23, 59, 59, 999), 0, countryID);
            trendThisMonthLastyear.trend = CommonFunctions.CompareTrend(trendThisMonthLastyear.totalLast, trendThisMonthLastyear.totalCurrent);
            result.trendThisMonthLastyear = trendThisMonthLastyear;
            // trendThisYearLastyear
            TrendCompare trendThisYearLastyear = new TrendCompare();
            dtTemp = dtTrend;
            trendThisYearLastyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999), 0, countryID);
            dtTemp = dtTrend.AddYears(-1);
            trendThisYearLastyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999), 0, countryID);
            trendThisYearLastyear.trend = CommonFunctions.CompareTrend(trendThisYearLastyear.totalLast, trendThisMonthLastyear.totalCurrent);
            result.trendThisYearLastyear = trendThisYearLastyear;
            // trendTodayThisyear
            TrendCompare trendTodayThisyear = new TrendCompare();
            dtTemp = dtTrend;
            trendTodayThisyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 23, 59, 59, 999), 0, countryID);
            dtTemp = dtTemp.AddDays(-1);
            trendTodayThisyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 23, 59, 59, 999), 0, countryID);
            trendTodayThisyear.trend = CommonFunctions.CompareTrend(trendTodayThisyear.totalLast, trendTodayThisyear.totalCurrent);
            result.trendTodayThisyear = trendTodayThisyear;
            // trendThisWeekThisyear
            TrendCompare trendThisWeekThisyear = new TrendCompare();
            dtTemp = CommonFunctions.GetFirstDayOfWeek(dtTrend);
            dtTempWeek = dtTemp.AddDays(7);
            trendThisWeekThisyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTempWeek.Year, dtTempWeek.Month, dtTempWeek.Day, 23, 59, 59, 999), 0, countryID);
            // random sub day from 1-6
            dtTemp = CommonFunctions.GetFirstDayOfWeek(dtTemp.AddDays(-4));
            dtTempWeek = dtTemp.AddDays(7);
            trendThisWeekThisyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTempWeek.Year, dtTempWeek.Month, dtTempWeek.Day, 23, 59, 59, 999), 0, countryID);
            trendThisWeekThisyear.trend = CommonFunctions.CompareTrend(trendThisWeekThisyear.totalLast, trendThisWeekThisyear.totalCurrent);
            result.trendThisWeekThisyear = trendThisWeekThisyear;
            // trendThisMonthThisyear
            TrendCompare trendThisMonthThisyear = new TrendCompare();
            dtTemp = dtTrend;
            trendThisMonthThisyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, DateTime.DaysInMonth(dtTemp.Year, dtTemp.Month), 23, 59, 59, 999), 0, countryID);
            dtTemp = dtTrend.AddMonths(-1);
            trendThisMonthThisyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, DateTime.DaysInMonth(dtTemp.Year, dtTemp.Month), 23, 59, 59, 999), 0, countryID);
            trendThisMonthThisyear.trend = CommonFunctions.CompareTrend(trendThisMonthThisyear.totalLast, trendThisMonthThisyear.totalCurrent);
            result.trendThisMonthThisyear = trendThisMonthThisyear;
            // trendThisYearThisyear
            result.trendThisYearThisyear = trendThisYearLastyear;
            // TrendCompareYear trendYear1 // trendYear2 // trendYear3 // trendYear4 // trendYear5
            // trendYear1
            TrendCompareYear tcYear1 = new TrendCompareYear();
            dtTemp = dtTrend.AddYears(-4);
            tcYear1.year = dtTemp.Year;
            tcYear1.total = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999), 0, countryID);
            tcYear1.trend = 0;
            result.trendYear1 = tcYear1;
            // trendYear2
            TrendCompareYear tcYear2 = new TrendCompareYear();
            dtTemp = dtTrend.AddYears(-3);
            tcYear2.year = dtTemp.Year;
            tcYear2.total = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999), 0, countryID);
            tcYear2.trend = CommonFunctions.CompareTrend(tcYear1.total, tcYear2.total);
            result.trendYear2 = tcYear2;
            // trendYear3
            TrendCompareYear tcYear3 = new TrendCompareYear();
            dtTemp = dtTrend.AddYears(-2);
            tcYear3.year = dtTemp.Year;
            tcYear3.total = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999), 0, countryID);
            tcYear3.trend = CommonFunctions.CompareTrend(tcYear2.total, tcYear3.total);
            result.trendYear3 = tcYear3;
            // trendYear4
            TrendCompareYear tcYear4 = new TrendCompareYear();
            dtTemp = dtTrend.AddYears(-1);
            tcYear4.year = dtTemp.Year;
            tcYear4.total = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999), 0, countryID);
            tcYear4.trend = CommonFunctions.CompareTrend(tcYear3.total, tcYear4.total);
            result.trendYear4 = tcYear4;
            // trendYear5
            TrendCompareYear tcYear5 = new TrendCompareYear();
            dtTemp = dtTrend;
            tcYear5.year = dtTemp.Year;
            tcYear5.total = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999), 0, countryID);
            tcYear5.trend = CommonFunctions.CompareTrend(tcYear4.total, tcYear5.total);
            result.trendYear5 = tcYear5;
            return result;
        }

        public static TrendNewsModel GetTrendNewsRegion(DateTime dtTrend, int regionID)
        {
            TrendNewsModel result = new TrendNewsModel();
            // trendTodayLastyear
            TrendCompare trendTodayLastyear = new TrendCompare();
            DateTime dtTemp = dtTrend;
            trendTodayLastyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 23, 59, 59, 999), regionID, 0);
            dtTemp = dtTrend.AddYears(-1);
            trendTodayLastyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 23, 59, 59, 999), regionID, 0);
            trendTodayLastyear.trend = CommonFunctions.CompareTrend(trendTodayLastyear.totalLast, trendTodayLastyear.totalCurrent);
            result.trendTodayLastyear = trendTodayLastyear;
            // trendThisWeekLastyear
            TrendCompare trendThisWeekLastyear = new TrendCompare();
            dtTemp = CommonFunctions.GetFirstDayOfWeek(dtTrend);
            DateTime dtTempWeek = dtTemp.AddDays(7);
            trendThisWeekLastyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTempWeek.Year, dtTempWeek.Month, dtTempWeek.Day, 23, 59, 59, 999), regionID, 0);
            dtTemp = CommonFunctions.GetFirstDayOfWeek(dtTrend.AddYears(-1));
            dtTempWeek = dtTemp.AddDays(7);
            trendThisWeekLastyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTempWeek.Year, dtTempWeek.Month, dtTempWeek.Day, 23, 59, 59, 999), regionID, 0);
            trendThisWeekLastyear.trend = CommonFunctions.CompareTrend(trendThisWeekLastyear.totalLast, trendThisWeekLastyear.totalCurrent);
            result.trendThisWeekLastyear = trendThisWeekLastyear;
            // trendThisMonthLastyear
            TrendCompare trendThisMonthLastyear = new TrendCompare();
            dtTemp = dtTrend;
            trendThisMonthLastyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, DateTime.DaysInMonth(dtTemp.Year, dtTemp.Month), 23, 59, 59, 999), regionID, 0);
            dtTemp = dtTrend.AddYears(-1);
            trendThisMonthLastyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, DateTime.DaysInMonth(dtTemp.Year, dtTemp.Month), 23, 59, 59, 999), regionID, 0);
            trendThisMonthLastyear.trend = CommonFunctions.CompareTrend(trendThisMonthLastyear.totalLast, trendThisMonthLastyear.totalCurrent);
            result.trendThisMonthLastyear = trendThisMonthLastyear;
            // trendThisYearLastyear
            TrendCompare trendThisYearLastyear = new TrendCompare();
            dtTemp = dtTrend;
            trendThisYearLastyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999), regionID, 0);
            dtTemp = dtTrend.AddYears(-1);
            trendThisYearLastyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999), regionID, 0);
            trendThisYearLastyear.trend = CommonFunctions.CompareTrend(trendThisYearLastyear.totalLast, trendThisMonthLastyear.totalCurrent);
            result.trendThisYearLastyear = trendThisYearLastyear;
            // trendTodayThisyear
            TrendCompare trendTodayThisyear = new TrendCompare();
            dtTemp = dtTrend;
            trendTodayThisyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 23, 59, 59, 999), regionID, 0);
            dtTemp = dtTemp.AddDays(-1);
            trendTodayThisyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 23, 59, 59, 999), regionID, 0);
            trendTodayThisyear.trend = CommonFunctions.CompareTrend(trendTodayThisyear.totalLast, trendTodayThisyear.totalCurrent);
            result.trendTodayThisyear = trendTodayThisyear;
            // trendThisWeekThisyear
            TrendCompare trendThisWeekThisyear = new TrendCompare();
            dtTemp = CommonFunctions.GetFirstDayOfWeek(dtTrend);
            dtTempWeek = dtTemp.AddDays(7);
            trendThisWeekThisyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTempWeek.Year, dtTempWeek.Month, dtTempWeek.Day, 23, 59, 59, 999), regionID, 0);
            // random sub day from 1-6
            dtTemp = CommonFunctions.GetFirstDayOfWeek(dtTemp.AddDays(-4));
            dtTempWeek = dtTemp.AddDays(7);
            trendThisWeekThisyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTempWeek.Year, dtTempWeek.Month, dtTempWeek.Day, 23, 59, 59, 999), regionID, 0);
            trendThisWeekThisyear.trend = CommonFunctions.CompareTrend(trendThisWeekThisyear.totalLast, trendThisWeekThisyear.totalCurrent);
            result.trendThisWeekThisyear = trendThisWeekThisyear;
            // trendThisMonthThisyear
            TrendCompare trendThisMonthThisyear = new TrendCompare();
            dtTemp = dtTrend;
            trendThisMonthThisyear.totalCurrent = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, DateTime.DaysInMonth(dtTemp.Year, dtTemp.Month), 23, 59, 59, 999), regionID, 0);
            dtTemp = dtTrend.AddMonths(-1);
            trendThisMonthThisyear.totalLast = GetCountChart(new DateTime(dtTemp.Year, dtTemp.Month, dtTemp.Day, 0, 0, 0, 0), new DateTime(dtTemp.Year, dtTemp.Month, DateTime.DaysInMonth(dtTemp.Year, dtTemp.Month), 23, 59, 59, 999), regionID, 0);
            trendThisMonthThisyear.trend = CommonFunctions.CompareTrend(trendThisMonthThisyear.totalLast, trendThisMonthThisyear.totalCurrent);
            result.trendThisMonthThisyear = trendThisMonthThisyear;
            // trendThisYearThisyear
            result.trendThisYearThisyear = trendThisYearLastyear;
            // TrendCompareYear trendYear1 // trendYear2 // trendYear3 // trendYear4 // trendYear5
            // trendYear1
            TrendCompareYear tcYear1 = new TrendCompareYear();
            dtTemp = dtTrend.AddYears(-4);
            tcYear1.year = dtTemp.Year;
            tcYear1.total = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999), regionID, 0);
            tcYear1.trend = 0;
            result.trendYear1 = tcYear1;
            // trendYear2
            TrendCompareYear tcYear2 = new TrendCompareYear();
            dtTemp = dtTrend.AddYears(-3);
            tcYear2.year = dtTemp.Year;
            tcYear2.total = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999), regionID, 0);
            tcYear2.trend = CommonFunctions.CompareTrend(tcYear1.total, tcYear2.total);
            result.trendYear2 = tcYear2;
            // trendYear3
            TrendCompareYear tcYear3 = new TrendCompareYear();
            dtTemp = dtTrend.AddYears(-2);
            tcYear3.year = dtTemp.Year;
            tcYear3.total = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999), regionID, 0);
            tcYear3.trend = CommonFunctions.CompareTrend(tcYear2.total, tcYear3.total);
            result.trendYear3 = tcYear3;
            // trendYear4
            TrendCompareYear tcYear4 = new TrendCompareYear();
            dtTemp = dtTrend.AddYears(-1);
            tcYear4.year = dtTemp.Year;
            tcYear4.total = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999), regionID, 0);
            tcYear4.trend = CommonFunctions.CompareTrend(tcYear3.total, tcYear4.total);
            result.trendYear4 = tcYear4;
            // trendYear5
            TrendCompareYear tcYear5 = new TrendCompareYear();
            dtTemp = dtTrend;
            tcYear5.year = dtTemp.Year;
            tcYear5.total = GetCountChart(new DateTime(dtTemp.Year, 1, 1, 0, 0, 0, 0), new DateTime(dtTemp.Year, 12, 31, 23, 59, 59, 999), regionID, 0);
            tcYear5.trend = CommonFunctions.CompareTrend(tcYear4.total, tcYear5.total);
            result.trendYear5 = tcYear5;
            return result;
        }
    }
}